<template>
  <div :class="showTotal ? 'total-introduce' : 'detailed-introduce'">
    <div
      :class="type=='comment' ? 'intro-content1' : 'intro-content2'"
      :title="introduce"
      ref="desc"
    >
      <strong
        style="color: #576b95;margin-right: .2rem;"
        v-if='strongLabel'
      >{{strongLabel}}</strong>
      <span
        class="merchant-desc"
        v-if="introduce"
      >
        {{ introduce }}
        <font
          style="color: #288ef4;"
          v-if="a"
        > {{GetDept}}</font>
        <font
          style="color: #288ef4;"
          v-if="!a"
        > @{{isPublic == 0 ? '自己':'全体员工'}}</font>
      </span>
      <div
        class="unfold"
        @click="showTotalIntro"
        v-if="showExchangeButton"
      >
        <p>{{ exchangeButton ? "全文" : "收起" }}</p>
      </div>
    </div>
  </div>
</template>

<script>
/**
 * 任务卡组件
 * 传入数据 t
 * {
        styleType: "task"=任务列表中文本, “comment”=留言板中文本
        t: "String", 文本内容
        a: "String", 需要@的名字
        strongLabel: 强化文本头
    }
 */
import maps from "@/util/maps";
export default {
  name: "MyContext",
  props: {
    t: String,
    a: String,
    isPublic: Number,
    styleType: String,
    strongLabel: String
  },
  components: {},
  data() {
    return {
      GetDept:'',
      type: "",
      introduce: "",
      // 是否展示所有文本内容
      showTotal: true,
      // 显示展开还是收起
      exchangeButton: true,
      // 是否显示展开收起按钮
      showExchangeButton: false,
      rem: ""
    };
  },
  mounted() {
    this.introduce = this.t;
    this.type = this.styleType || "task";
    this.strongText = this.strongLabel || "";
    if (this.a) {
      maps.getTargetDeptIds(this.a).then(e => {
        this.GetDept = e;
      });
    }
  },
  methods: {
    showTotalIntro() {
      //console.log(this.showTotal);
      this.showTotal = !this.showTotal;
      this.exchangeButton = !this.exchangeButton;
    },
    getRem() {
      const defaultRem = 16;
      let winWidth = window.innerWidth;
      //console.log("winWidth:" + winWidth);
      let rem = (winWidth / 375) * defaultRem;
      return rem;
    }
  },
  watch: {
    introduce: function() {
      this.$nextTick(
        function() {
          //console.log("nextTick");
          // 判断介绍是否超过四行
          let rem = parseFloat(this.getRem());
          //console.log("watch 中的rem", rem);
          if (!this.$refs.desc) {
            //console.log("desc null");
            return;
          }
          let descHeight = window
            .getComputedStyle(this.$refs.desc)
            .height.replace("px", "");
          // //console.log("descHeight:" + descHeight);
          //console.log("如果 descHeight 超过" + 1.25 * rem + "就要显示展开按钮");
          if (descHeight > 2.12 * rem) {
            //console.log("超过了四行");
            // 显示展开收起按钮
            this.showExchangeButton = true;
            this.exchangeButton = true;
            // 不是显示所有
            this.showTotal = false;
          } else {
            // 不显示展开收起按钮
            this.showExchangeButton = false;
            // 没有超过四行就显示所有
            this.showTotal = true;
            // //console.log("showExchangeButton", this.showExchangeButton);
            // //console.log("showTotal", this.showTotal);
          }
        }.bind(this)
      );
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less" >
@zi : 30px;
@h-zi: @zi + 10px;
@mg : 0;
@clickWidth: 100px;
.total-introduce {
  height: auto;
  overflow: hidden;
  font-size: @zi;
  color: #333;
  margin: @mg;
  .intro-content {
    .merchant-desc {
      width: 100%;
      line-height: @h-zi;
    }
  }
  .unfold {
    display: block;
    z-index: 11;
    float: right;
    width: @clickWidth;
    text-align: center;
    padding: 10px 0;
    p {
      margin: 0;
      line-height: @zi;
      color: #7fbe87;
    }
  }
}
.detailed-introduce {
  height: @zi * 6.5;
  font-size: @zi;
  color: #333;
  position: relative;
  overflow: hidden;
  margin: @mg;
  .intro-content1 {
    // 最大高度设为4倍的行间距
    // max-height: @h-zi * 5;
    line-height: @h-zi;
    word-wrap: break-word;
    /*强制打散字符*/
    word-break: break-all;
    background: #ffffff;
    /*同背景色*/
    color: #ffffff;
    overflow: hidden;
    .merchant-desc {
      width: 100%;
      line-height: @h-zi;
    }
    &:after,
        // 这是展开前实际显示的内容
      &:before {
      content: attr(title);
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      color: #434343;
      overflow: hidden;
    }
    // 把最后最后一行自身的上面三行遮住
    &:before {
      display: block;
      overflow: hidden;
      z-index: 1;
      // max-height: @h-zi * 5;
      background: #f7f7f7;
    }
    &:after {
      display: -webkit-box;
      -webkit-box-orient: vertical;
      overflow: hidden;
      height: @h-zi;
      /*截取行数*/
      -webkit-line-clamp: 4;
      text-overflow: ellipsis;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
      /*行首缩进字符数，值为[(截取行数-1)*尾部留空字符数]*/
      text-indent: -24px;
      /*尾部留空字符数*/
      padding-right: 30px;
    }
    .unfold {
      z-index: 11;
      width: @clickWidth;
      outline: 0;
      position: absolute;
      right: 0;
      bottom: 0;
      text-align: center;
      p {
        background: #f7f7f7;
        // margin-left: -30px;
        line-height: @h-zi;
        color: #576b95;
        font-weight: 400;
      }
    }
  }
  .intro-content2 {
    // 最大高度设为4倍的行间距
    // max-height: @h-zi * 5;
    line-height: @h-zi;
    word-wrap: break-word;
    /*强制打散字符*/
    word-break: break-all;
    background: #ffffff;
    /*同背景色*/
    color: #ffffff;
    overflow: hidden;
    .merchant-desc {
      width: 100%;
      line-height: @h-zi;
    }
    &:after,
        // 这是展开前实际显示的内容
      &:before {
      content: attr(title);
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      color: #434343;
      overflow: hidden;
    }
    // 把最后最后一行自身的上面三行遮住
    &:before {
      display: block;
      overflow: hidden;
      z-index: 1;
      // max-height: @h-zi * 5;
      background: #fff;
    }
    &:after {
      display: -webkit-box;
      -webkit-box-orient: vertical;
      overflow: hidden;
      height: @h-zi;
      /*截取行数*/
      -webkit-line-clamp: 4;
      text-overflow: ellipsis;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
      /*行首缩进字符数，值为[(截取行数-1)*尾部留空字符数]*/
      text-indent: -24px;
      /*尾部留空字符数*/
      padding-right: 30px;
    }
    .unfold {
      z-index: 11;
      width: @clickWidth;
      outline: 0;
      position: absolute;
      right: 0;
      bottom: 0;
      text-align: center;
      p {
        background: #fff;
        line-height: @h-zi;
        color: #576b95;
        font-weight: 400;
      }
    }
  }
}
</style>
